package org.zhaozhen.user.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.zhaozhen.basic.Constant.LoginConstant;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.basic.util.MD5Utils;
import org.zhaozhen.basic.util.StrUtils;
import org.zhaozhen.org.mapper.EmployeeMapper;
import org.zhaozhen.user.domain.Logininfo;
import org.zhaozhen.user.domain.User;
import org.zhaozhen.user.dto.UserDto;
import org.zhaozhen.user.mapper.LogininfoMapper;
import org.zhaozhen.user.mapper.UserMapper;
import org.zhaozhen.user.service.IUserService;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    //1.基本CRUD的实现,通过继承得到

    //2.特定的业务实现,在这里写

    /**
     * 用户注册
     * @param dto
     */
    @Override
    public void register(UserDto dto) {
        //校验参数不能为空
        if (StringUtils.isEmpty(dto.getVerifyCode())||StringUtils.isEmpty(dto.getPassword())||StringUtils.isEmpty(dto.getConfirmPassword())){
            throw new BusinessException("必要参数不能为空!!");
        }
        //验证密码和确认密码一致
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinessException("密码和确认密码不一致!!");
        }
        //验证手机没有被注册国
        User user = userMapper.loadByPhone(dto.getPhone());
        if (user!=null){
            throw new BusinessException("该用户已注册!!");
        }
        //去Redis中拿验证码
        Object verifyCode = redisTemplate.opsForValue().get(LoginConstant.PHONE_REGISTER + dto.getPhone());
        //判断是否可以拿到
        if (verifyCode==null){
            throw new BusinessException("请重新获取验证码!!");
        }
        String yzm = verifyCode.toString().split(":")[0];
        if (!dto.getVerifyCode().equals(yzm)){
            throw new BusinessException("验证码输入不正确!!");
        }
        //保存数据，需要保存user和logininfo表
        User user1 = userDto2User(dto);
        Logininfo logininfo = user2Logininfo(user1);
        // 1.t_logininfo 返回自增Id
        logininfoMapper.save(logininfo);
        // 2.保存t_user
        user1.setLogininfo_id(logininfo.getId());
        userMapper.save(user1);
    }

    /**
     * 把user转换成logininfo
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//注意:一定要设置
        return logininfo;
    }

    /**
     * 将dto对象转换为user对象
     * @param dto
     * @return
     */
    private User userDto2User(UserDto dto) {
        //准备放回对象
        User user = new User();
        //设置值
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //通过盐值加密密码
        String md5Password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);//保存盐值
        user.setPassword(md5Password);//加密之后的密码
        //返回这个对象
        return user;
    }

}
